using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Extensions
{
    /// <summary>
    /// 数据库扩展方法
    /// </summary>
    public static class DatabaseExtensions
    {
        /// <summary>
        /// 添加核心仓储服务
        /// </summary>
        public static IServiceCollection AddCoreRepositories(this IServiceCollection services)
        {
            // 核心仓储服务注册
            // 这里可以添加基本的仓储模式实现
            
            return services;
        }

        /// <summary>
        /// 添加所有仓储服务
        /// </summary>
        public static IServiceCollection AddModularRepositories(this IServiceCollection services)
        {
            // 所有仓储服务注册
            // 这里可以添加完整的仓储模式实现
            
            return services;
        }
    }

    /// <summary>
    /// 懒加载数据库上下文工厂接口
    /// </summary>
    public interface ILazyDbContextFactory<T> where T : DbContext
    {
        T CreateDbContext();
        Task<T> CreateDbContextAsync();
    }

    /// <summary>
    /// 懒加载数据库上下文工厂实现
    /// </summary>
    public class LazyDbContextFactory<T> : ILazyDbContextFactory<T> where T : DbContext
    {
        private readonly IServiceProvider _serviceProvider;

        public LazyDbContextFactory(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public T CreateDbContext()
        {
            return _serviceProvider.GetRequiredService<T>();
        }

        public Task<T> CreateDbContextAsync()
        {
            return Task.FromResult(_serviceProvider.GetRequiredService<T>());
        }
    }
}
